﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.IO;
using System.Windows;
using System.Xml.Serialization;
using System.Xml;

namespace E_mail
{
    public class AccountPropertis
    {
        public AccountData dane = new AccountData();
        
        public const String fileName = @"Account.xml";
        public String Password;
        public AccountPropertis()
        {        
            if (!File.Exists(fileName))
            {
                return;
            }
            LoadAccountPropertis();
        }

        public void LoadAccountPropertis()
        {
            string s = Assembly.GetExecutingAssembly().Location; //+"\\" + fileName;
            var length = s.IndexOf("E-mail.exe");
            s = s.Substring(0,length) + fileName;
            if (!File.Exists(s))
            {
                return;
            }
                var sr = new StreamReader(s);
                var ss = new XmlSerializer(typeof(AccountData));
                dane = (AccountData)ss.Deserialize(sr);
                sr.Close();
        }

        public void Save(String UserName,String Password,String ServerName,int Port)
        {
            var tmp = new AccountData(){UserName = UserName,ServerAdress = ServerName,Port = Port};
            var s = new XmlSerializer(typeof(AccountData));
            var sw = new StreamWriter(fileName); 
            s.Serialize(sw,tmp);
            sw.Close();
        }
        public void Save()
        {
            try
            {
                string filePath = Assembly.GetExecutingAssembly().Location;
                var length = filePath.IndexOf("E-mail.exe");
                filePath = filePath.Substring(0, length) + fileName;
                var s = new XmlSerializer(typeof(AccountData));
                var sw = new StreamWriter(filePath);
                s.Serialize(sw, dane);
                sw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
